﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using GrFingerXLib;
using LibHuman;


namespace Human
{
	/// <summary>
	/// Summary description for FrmNhanDang.
	/// </summary>
	public class FrmNhanDang : System.Windows.Forms.Form
    {
        Language lan = new Language();
		private UtilFinger m_util;
		public string mabn="";
		private bool _group=false;
		private string idsensor="";
		private PictureBox pb;
		private Image img;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.PictureBox pb1;
		private System.Windows.Forms.Label lblstatus;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label lblscore;
		private System.Windows.Forms.Label lblidentify;
		private System.Windows.Forms.Button btnchapnhan;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
        private System.Windows.Forms.ToolTip toolTip1;
        private AxGrFingerXLib.AxGrFingerXCtrl gr;
        private Button btnExtract;
		private System.ComponentModel.IContainer components;

		public FrmNhanDang(PictureBox pb )
		{
			
			
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
			
			toolTip1.SetToolTip(btnchapnhan,lan.Change_language_MessageText("Enter để đồng ý nếu như hình ảnh \n không đủ dữ liệu để phân tích"));
			toolTip1.SetToolTip(label1,lan.Change_language_MessageText("Số điểm có được bởi sự phân tích hình ảnh của chương trình"));
			toolTip1.SetToolTip(lblscore,lan.Change_language_MessageText("Số điểm có được bởi sự phân tích hình ảnh của chương trình"));
			this.pb=pb;//picture bên form tiepdon
			int ret;
			m_util=new UtilFinger(lblstatus,pb1);
			ret=m_util.InitialGrFinger(gr);
			if(ret<0)
				m_util.MessageError((GrFingerXLib.GRConstants)ret);
			else
				lblstatus.Text=lan.Change_language_MessageText("Yêu cầu đặt ngón tay lên thiết bị.");
			gr.SetIdentifyParameters(20,180,(int)GrFingerXLib.GRConstants.GR_DEFAULT_CONTEXT);
            //_group = true;
			menuItem1.Visible=(_group);
			
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			try
			{
				foreach(Control c in this.Controls)
				{
					if(c!=null) c.Dispose();
				}

				if( disposing )
				{
					if(components != null)
					{
						components.Dispose();
					}
				}
			}
			catch{}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmNhanDang));
            this.pb1 = new System.Windows.Forms.PictureBox();
            this.label1 = new System.Windows.Forms.Label();
            this.lblstatus = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.lblscore = new System.Windows.Forms.Label();
            this.lblidentify = new System.Windows.Forms.Label();
            this.btnchapnhan = new System.Windows.Forms.Button();
            this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.gr = new AxGrFingerXLib.AxGrFingerXCtrl();
            this.btnExtract = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.pb1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.gr)).BeginInit();
            this.SuspendLayout();
            // 
            // pb1
            // 
            this.pb1.BackColor = System.Drawing.SystemColors.Window;
            this.pb1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pb1.Location = new System.Drawing.Point(4, 27);
            this.pb1.Name = "pb1";
            this.pb1.Size = new System.Drawing.Size(112, 160);
            this.pb1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pb1.TabIndex = 1;
            this.pb1.TabStop = false;
           
            // 
            // label1
            // 
            this.label1.ForeColor = System.Drawing.Color.DodgerBlue;
            this.label1.Location = new System.Drawing.Point(120, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(88, 24);
            this.label1.TabIndex = 2;
            this.label1.Text = "Score:";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // lblstatus
            // 
            this.lblstatus.BackColor = System.Drawing.SystemColors.Info;
            this.lblstatus.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblstatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblstatus.ForeColor = System.Drawing.Color.OrangeRed;
            this.lblstatus.Location = new System.Drawing.Point(124, 27);
            this.lblstatus.Name = "lblstatus";
            this.lblstatus.Size = new System.Drawing.Size(112, 160);
            this.lblstatus.TabIndex = 3;
            this.lblstatus.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.lblstatus.Click += new System.EventHandler(this.lblstatus_Click);
            // 
            // label2
            // 
            this.label2.ForeColor = System.Drawing.Color.DodgerBlue;
            this.label2.Location = new System.Drawing.Point(0, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(120, 24);
            this.label2.TabIndex = 2;
            this.label2.Text = "Hình ảnh:";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // lblscore
            // 
            this.lblscore.BackColor = System.Drawing.Color.PapayaWhip;
            this.lblscore.ForeColor = System.Drawing.Color.DodgerBlue;
            this.lblscore.Location = new System.Drawing.Point(203, 0);
            this.lblscore.Name = "lblscore";
            this.lblscore.Size = new System.Drawing.Size(32, 24);
            this.lblscore.TabIndex = 2;
            this.lblscore.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // lblidentify
            // 
            this.lblidentify.BackColor = System.Drawing.Color.PapayaWhip;
            this.lblidentify.ForeColor = System.Drawing.Color.DodgerBlue;
            this.lblidentify.Location = new System.Drawing.Point(248, 40);
            this.lblidentify.Name = "lblidentify";
            this.lblidentify.Size = new System.Drawing.Size(32, 24);
            this.lblidentify.TabIndex = 2;
            this.lblidentify.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // btnchapnhan
            // 
            this.btnchapnhan.Enabled = false;
            this.btnchapnhan.Location = new System.Drawing.Point(40, 227);
            this.btnchapnhan.Name = "btnchapnhan";
            this.btnchapnhan.Size = new System.Drawing.Size(80, 24);
            this.btnchapnhan.TabIndex = 4;
            this.btnchapnhan.Text = "Chấp Nhận";
            this.btnchapnhan.Click += new System.EventHandler(this.btnchapnhan_Click);
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem1});
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 0;
            this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem2,
            this.menuItem3});
            this.menuItem1.Text = "&Tiện ích";
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 0;
            this.menuItem2.Text = "&Liệt kê danh sách vân tay";
            this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
            // 
            // menuItem3
            // 
            this.menuItem3.Index = 1;
            this.menuItem3.Text = "&Map ổ đĩa";
            this.menuItem3.Visible = false;
            this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
            // 
            // gr
            // 
            this.gr.Enabled = true;
            this.gr.Location = new System.Drawing.Point(177, 139);
            this.gr.Name = "gr";
            this.gr.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("gr.OcxState")));
            this.gr.Size = new System.Drawing.Size(32, 32);
            this.gr.TabIndex = 23;
            this.gr.SensorPlug += new AxGrFingerXLib._IGrFingerXCtrlEvents_SensorPlugEventHandler(this.gr_SensorPlug);
            this.gr.SensorUnplug += new AxGrFingerXLib._IGrFingerXCtrlEvents_SensorUnplugEventHandler(this.gr_SensorUnplug);
            this.gr.ImageAcquired += new AxGrFingerXLib._IGrFingerXCtrlEvents_ImageAcquiredEventHandler(this.gr_ImageAcquired);
            // 
            // btnExtract
            // 
            this.btnExtract.Location = new System.Drawing.Point(127, 227);
            this.btnExtract.Name = "btnExtract";
            this.btnExtract.Size = new System.Drawing.Size(88, 24);
            this.btnExtract.TabIndex = 24;
            this.btnExtract.Text = "Extract";
            this.btnExtract.Click += new System.EventHandler(this.btnExtract_Click);
            // 
            // FrmNhanDang
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(243, 192);
            this.Controls.Add(this.btnExtract);
            this.Controls.Add(this.gr);
            this.Controls.Add(this.btnchapnhan);
            this.Controls.Add(this.lblscore);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.lblstatus);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.lblidentify);
            this.Controls.Add(this.pb1);
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.KeyPreview = true;
            this.MaximizeBox = false;
            this.Menu = this.mainMenu1;
            this.MinimizeBox = false;
            this.Name = "FrmNhanDang";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Xác nhận";
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmNhanDang_KeyDown);
            this.Load += new System.EventHandler(this.FrmNhanDang_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pb1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.gr)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion


		public string save_orac(string mabn)
		{
            string gttv = "";
            if (m_util.TemplateIsValid())
            {
                System.Array arrtemp = new byte[(int)m_util.tpt._size];
                System.Array.Copy(m_util.tpt._tpt, 0, arrtemp, 0, m_util.tpt._size);
                gttv= m_util.upd_vantay(mabn);
            }
           else
           {
                //MessageBox.Show("Lổi không lưu được vắn tay!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
               gttv = lan.Change_language_MessageText("Lổi không lưu được vân tay!");
           }
			pb.Image=null;
			pb1.Image=null;
			lblstatus.Text=lan.Change_language_MessageText("Yêu cầu đặt ngón tay lên thiết bị.");
			this.Refresh();
			this.Visible=false;
            return gttv;
		}
		private void gr_SensorPlug(object sender, AxGrFingerXLib._IGrFingerXCtrlEvents_SensorPlugEvent e)
		{
			idsensor=e.idSensor;
			gr.CapStartCapture(e.idSensor);
		}
		private void gr_SensorUnplug(object sender, AxGrFingerXLib._IGrFingerXCtrlEvents_SensorUnplugEvent e)
		{
			gr.CapStopCapture(e.idSensor);
		}
		private void gr_ImageAcquired(object sender, AxGrFingerXLib._IGrFingerXCtrlEvents_ImageAcquiredEvent e)
		{
			int ret;
			if(this.Visible==false)return;
			m_util.raw.img=e.rawImage;
			m_util.raw.height=e.height;
			m_util.raw.width=e.width;
			m_util.raw.Res=e.res;
			
            try
            {
                
                m_util.PrintBiometricDisplay(false, GRConstants.GR_DEFAULT_CONTEXT);
            }
            catch (Exception ex)
            {
                lblstatus.Text=ex.Message;
            }
			img=(Image)pb1.Image.Clone();
			//extract
            //btnExtract.PerformClick();
            //MessageBox.Show(ExtractTemplate().ToString());
            ret=ExtractTemplate();
            if(ret<1)
            {
                lblstatus.Text=lan.Change_language_MessageText("Hình ảnh vân tay không đủ dữ liệu để phân tích.Yêu cầu đặt lại hoặc chấp nhận");
                this.Refresh();
                return;
            }
			ret=m_util.ExtractTemplate();
			ret=m_util.Verify();
			
			lblscore.Text=ret.ToString();
            this.Refresh();
            if (ret < m_util.Score)
            {
                lblstatus.Text = lan.Change_language_MessageText("Hình ảnh vân tay không đủ dữ liệu để phân tích.Yêu cầu đặt lại hoặc chấp nhận");
                this.Refresh();
                return;
            }

            Tim();
		}
		private void Tim()
		{
			lblstatus.Text=lan.Change_language_MessageText("Đang tìm kiếm. Vui lòng đợi...");
			this.Refresh();
			m_util.PrintBiometricDisplay(true,GrFingerXLib.GRConstants.GR_NO_CONTEXT);
			int score=0;
			mabn="";
			mabn=m_util.Identify(ref score);

            if (mabn != "")
                pb.Image = img;//gán hình ngược trở lại cho formchinh
            else
                pb.Image = null;

			pb1.Image=null;
			lblstatus.Text=lan.Change_language_MessageText("Yêu cầu đặt ngón tay lên thiết bị.");
			this.Refresh();
			lblidentify.Text=score.ToString();
			this.Visible=false;
		}

		private void btnchapnhan_Click(object sender, System.EventArgs e)
		{
			Tim();
		}
		public System.Drawing.Image getImageFromMabn(string ma)
		{
			
            return m_util.getImage(ma);
		}

		private void FrmNhanDang_Load(object sender, System.EventArgs e)
		 {
		
		}

		private void FrmNhanDang_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.Control && e.Alt && e.Shift && (e.KeyCode==Keys.A))
			{		
				menuItem1.Visible=!menuItem1.Visible;
				menuItem2.Visible=!menuItem2.Visible;
				menuItem3.Visible=!menuItem3.Visible;
			}
		}

        private int ExtractTemplate()
        {
            int ret=0;

            // extract template
            ret = m_util.ExtractTemplate();
            // write template quality to the log
            if ((GRConstants)ret == GRConstants.GR_BAD_QUALITY)
            {
                lblstatus.Text = lan.Change_language_MessageText("Không tốt");
            }
            else if ((GRConstants)ret == GRConstants.GR_MEDIUM_QUALITY)
            {
                lblstatus.Text = lan.Change_language_MessageText("Trung bình");
            }
            else if ((GRConstants)ret == GRConstants.GR_HIGH_QUALITY)
            {
                lblstatus.Text = lan.Change_language_MessageText("Khá tốt");
            }
            if (ret >= 0)
            {
                // if no error, display minutiae/segments/directions into image
                m_util.PrintBiometricDisplay(true, GRConstants.GR_NO_CONTEXT);
                
            }
            else
            {
                lblstatus.Text = ((GRConstants)ret).ToString();
            }
            lblstatus.Refresh();
            return ret;
        }
        private void btnExtract_Click(object sender, EventArgs e)
        {
            int ret;
            
            // extract template
            ret = m_util.ExtractTemplate();
            // write template quality to the log
            if ((GRConstants)ret == GRConstants.GR_BAD_QUALITY)
            {
                lblstatus.Text = lan.Change_language_MessageText("Không tốt");
            }
            else if ((GRConstants)ret == GRConstants.GR_MEDIUM_QUALITY)
            {
                lblstatus.Text=lan.Change_language_MessageText("Trung bình");
            }
            else if ((GRConstants)ret == GRConstants.GR_HIGH_QUALITY)
            {
                lblstatus.Text = lan.Change_language_MessageText("Khá tốt");
            }
            if (ret >= 0)
            {
                // if no error, display minutiae/segments/directions into image
                m_util.PrintBiometricDisplay(true, GRConstants.GR_NO_CONTEXT);
                // enable operations we can do over extracted template
                //btnExtract.Enabled = false;
                //btEnroll.Enabled = true;
                //btIdentify.Enabled = true;
                //btVerify.Enabled = true;
            }
            else
            {
                // write error to the log
                lblstatus.Text=((GRConstants)ret).ToString();
            }
            lblstatus.Refresh();
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {

        }

        private void menuItem2_Click(object sender, EventArgs e)
        {

        }

        private void lblstatus_Click(object sender, EventArgs e)
        {

        }

        
	}
}
